﻿namespace DotRas.Design
{
    using DotRas;
    using System;
    using System.ComponentModel;

    [ToolboxItem(false)]
    public abstract class RasComponent : Component
    {
        private ISynchronizeInvoke _synchronizingObject;

        protected RasComponent()
        {
        }

        protected RasComponent(IContainer container)
        {
            if (container != null)
            {
                container.Add(this);
            }
        }

        protected virtual void InitializeComponent()
        {
        }

        protected void RaiseEvent<TEventArgs>(EventHandler<TEventArgs> method, TEventArgs e) where TEventArgs: EventArgs
        {
            if ((method != null) && this.CanRaiseEvents)
            {
                lock (method)
                {
                    if ((this.SynchronizingObject != null) && this.SynchronizingObject.InvokeRequired)
                    {
                        this.SynchronizingObject.Invoke(method, new object[] { this, e });
                    }
                    else
                    {
                        method(this, e);
                    }
                }
            }
        }

        [DotRas.SRDescription("RCSyncObjectDesc"), DefaultValue((string) null)]
        public ISynchronizeInvoke SynchronizingObject
        {
            get
            {
                return this._synchronizingObject;
            }
            set
            {
                this._synchronizingObject = value;
            }
        }
    }
}

